import request from '@/utils/request';
import { stringify } from 'qs';
import { getPageSize } from '@/utils/common';
import { async } from 'q';

export const token = localStorage.getItem('token');
export const user = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {};

/**
 * 查询订单管理列表
 */
export async function selectExportOSList(params) {
  params.pageSize = params.pageSize || getPageSize();
  return request(`/server/api/api/OCS/Order/GetMyOrderList?${stringify({ token })}`, {
    method: 'POST',
    body: params,
  });
}

/**
 * 查询订单管理详情
 */
export async function selectExportOSDetail(params) {
  params.token = token;
  return request(`/server/api/api/OCS/Order/GetOrderSingle?${stringify(params)}`);
}

/**
 *  保存(新增、编辑)
 */
export async function operateExportOS(params) {
  const { id, ...rest } = params;
  params.customerCode = user.customerCode;
  const url = id ? 'EditOrder' : 'CreateOrder';
  return request(
    `/server/api/api/OCS/Order/${url}?${stringify({ token })}`,
    {
      method: 'POST',
      body: params,
    },
    true
  );
}

//删除
export async function delExportOS(params) {
  return request(`/server/api/api/OCS/Order/DelOrder?${stringify({ token })}`, {
    method: 'POST',
    body: params,
  });
}
// 取消
export async function cancelOrder(params) {
  return request(`/server/api/api/OCS/Order/CancelOrder?${stringify({ token })}`, {
    method: 'POST',
    body: params,
  });
}

/**
 * 查询附件
 */
export async function selectFileList(params) {
  return request(`/server/api/tms/attachment/selectFileList`, {
    method: 'POST',
    body: params,
  });
}
//上传附件：
export async function uploadExportOS(params) {
  return request(`/server/api/api/OCS/Order/UploadFileTemp?${stringify({ token })}`, {
    method: 'POST',
    body: params,
  });
}
//附件补录
export async function operateOrderFile(params) {
  const { files, ...param } = params;
  param.token = token;
  return request(`/server/api/api/OCS/Order/EditOrderFile?${stringify(param)}`, {
    method: 'POST',
    body: files,
  });
}
//查询订单附件：
export async function getOrderFile(params) {
  params.token = token;
  return request(`/server/api/api/OCS/Order/GetOrderFile?${stringify(params)}`, {
    method: 'POST',
    body: {},
  });
}
//生成客户单号：
export async function getAutoOrderNo() {
  return request(`/server/api/api/OCS/Order/GenerateCustomerOrderNO?${stringify({ token })}`, {
    method: 'POST',
    body: {},
  });
}
//获取城市：
export async function getAreaByAddress(params) {
  return request(`/server/api/api/Common/Area/GetAreaByAddress?${stringify(params)}`, {
    method: 'POST',
    body: {},
  });
}

//地址列表
export async function contactList(params) {
  params.token = token;
  params.pageSize = params.pageSize || 10;
  params.pageIndex = params.pageIndex || 1;
  return request(`/server/api/api/Common/Address/GetContact?${stringify(params)}`, {
    method: 'POST',
    body: {},
  });
}

//地址新增、编辑
export async function operateAddress(params) {
  const url = params.id ? 'EditContact' : 'AddContact';
  return request(
    `/server/api/api/Common/Address/${url}?${stringify({ token })}`,
    {
      method: 'POST',
      body: params,
    },
    true
  );
}
//地址删除
export async function delContact(params) {
  return request(`/server/api/api/Common/Address/DelContact?${stringify({ token })}`, {
    method: 'POST',
    body: params,
  });
}
//地址识别
export async function discernAddress(params) {
  return request(`/server/api/api/Common/Area/AddressRecognition?${stringify(params)}`, {
    method: 'POST',
    body: {},
  });
}
//获取车型
// api/OCS/Order/GetTruckTypeList
export async function getTruckTypeList(params) {
  return request(`/server/api/api/OCS/Order/GetTruckTypeList?${stringify({ token })}`, {
    method: 'POST',
    body: {},
  });
}
